import React, { Component } from 'react-native';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';

import * as reducers from '../reducers';
import Bw800App from './bw800App';
import SplashScreen from '../components/splashScreen/splashScreen'

const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const reducer = combineReducers(reducers);
const store = createStoreWithMiddleware(reducer);

// 打印初始状态
console.log(store.getState());

// 监听 state 更新时，打印日志
// 注意 subscribe() 返回一个函数用来注销监听器
let unsubscribe = store.subscribe(() =>
  console.log(store.getState())
);
// 停止监听 state 更新
//unsubscribe();
//
export default class App extends Component {
	constructor(props) {
	    super(props);
	    //LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
	    this.state={
	    	splashed:false
	    }
	 }
	componentDidMount() {
	    this.timer = setTimeout(
	      () => { this.setState({splashed: true});; },
	      2000
	    );
	 }
	componentWillUnmount() {
	    // 如果存在this.timer，则使用clearTimeout清空。
	    // 如果你使用多个timer，那么用多个变量，或者用个数组来保存引用，然后逐个clear
	    this.timer && clearTimeout(this.timer);
	}
	render() {
	  	if (this.state.splashed) {
		  	return (
		      <Provider store={store}>
		        <Bw800App />
		      </Provider>
		    );
	  	}else{
		  	return (
		    	<SplashScreen />
		    );
	  	}
	}
}
